import { defineStore } from 'pinia'
import { AuthState } from '@/stores/interface'
import { fetchCurrentUserMenus } from '@/api/modules/uc/menu'
import { Menu } from '@api/uc/menu/interface'
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList } from '@/utils'
import { APPCODE } from '@/config'
export const useAuthStore = defineStore({
  id: 'stove-auth',
  state: (): AuthState => ({
    // 按钮权限列表
    authButtonList: {},
    // 菜单权限列表
    authMenuList: [],
    // 当前页面的 router name，用来做按钮权限筛选
    routeName: ''
  }),
  getters: {
    // 按钮权限列表
    authButtonListGet: state => state.authButtonList,
    // 菜单权限列表 ==> 这里的菜单没有经过任何处理
    authMenuListGet: state => state.authMenuList,
    // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
    showMenuListGet: state => getShowMenuList(state.authMenuList),
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: state => getFlatMenuList(state.authMenuList),
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
  },
  actions: {
    async initMenu() {
      const data = await fetchCurrentUserMenus(APPCODE)
      const menus = list2Tree(data)
      this.authMenuList = menus.menus
      this.authButtonList = menus.buttons
    },
    // Set RouteName
    async setRouteName(name: string) {
      this.routeName = name
    }
  }
})

const home = {
  id: 'home',
  parentId: '',
  type: '1',
  path: '/home/index',
  name: 'ZLHome',
  component: '/home/index',
  level: 1,
  meta: {
    icon: 'HomeFilled',
    title: '首页',
    isLink: false,
    isHide: false,
    isFull: false,
    isAffix: true,
    isKeepAlive: true
  }
}

function list2Tree(data: Menu.Record[]) {
  const idMap = new Map<string, MenuOptions>()
  const rootChildren: MenuOptions[] = [home]
  const authButtons: Record<string, string[]> = {}

  for (let item of data) {
    if (item.type === '4') {
      if (!item.name) {
        continue
      }
      const temp = authButtons[item.name]
      let authArray: string[]
      if (!temp) {
        authArray = []
        authButtons[item.name] = authArray
      } else {
        authArray = temp
      }

      if (item.permission) {
        authArray.push(item.permission)
      }
      continue
    }
    const menu: MenuOptions = {
      id: item.id,
      parentId: item.parentId,
      type: item.type,
      path: item.path ? item.path : item.id, // 必须有值且保证唯一，否则菜单显示有问题
      name: item.name,
      component: item.component,
      redirect: item.redirect,
      level: item.level,
      layoutName: item.layoutName,
      meta: {
        icon: item.icon,
        title: item.title,
        isLink: item.isLink === '1',
        isHide: item.isHide === '1',
        isFull: item.isFull === '1',
        isAffix: item.isAffix === '1',
        isKeepAlive: item.isKeepAlive === '1',
        map: item.showMap === '1'
      }
    }
    idMap.set(menu.id, menu)
  }
  // 迭代 Map 中的 value
  for (let item of idMap.values()) {
    if (item.level === 2) {
      rootChildren.push(item)
    } else {
      const parent = idMap.get(item.parentId)
      if (!parent) {
        continue
      }
      if (!parent.children) {
        parent.children = []
      }
      parent.children.push(item)
    }
  }
  return { menus: rootChildren, buttons: authButtons }
}
